package com.spider.silence.worker;

import com.spider.silence.abstarct.downloader.AbstractCommonDownLoader;
import com.spider.silence.proxy.ProxyPool;
import com.spider.silence.task.KaiDiModuleTask;
import com.spider.silence.task.KaiDiTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.PropertiesUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/1/22 0022
 */
public class KaiDiModuleWorker extends AbstractCommonDownLoader implements Runnable{
    private Logger logger = Logger.getLogger(KaiDiModuleWorker.class);

    @Override
    public void run() {

        int total_page = 0;

        while(true&&!Thread.interrupted()){
            try{
                KaiDiModuleTask moduleTask = (KaiDiModuleTask) CommonTaskManager.takeTask("KaiDiModule",1000);
                if(null!=moduleTask){
                    String module_url = moduleTask.getModuleUrl();
                    long future = System.currentTimeMillis() + Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                    long remianing = Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                    boolean flag = true;
                    List<Elements> elementsList = new ArrayList<Elements>();
                    try {
                        remianing = future - System.currentTimeMillis();
                        Document document = get(module_url, ProxyPool.proxy());
                        Elements threadList = document.select(".bbsLeft").select(".postbox").select(".postlist-table").select(".list-folder");
                        //Elements threadList = document.select(".wrapper").select(".list-table").select("tr[name^='showreply_']");
                        Elements pageElements = document.select(".wrapper").select(".operating").select(".pages").select(".pagesmodule");

                        if(threadList.size()>0&&threadList!=null){
                            for (int i = 0; i < threadList.size(); i++) {
                                String forumUrl = threadList.get(i).select(".table-title").select("a").get(0).attr("href");
                                KaiDiTask task = new KaiDiTask(URLDecoder.decode(forumUrl, "UTF-8"));
                                CommonTaskManager.addTask("KaiDiForum", task,1000);
                                System.out.println(forumUrl);
                            }
                        }

                        /*String pageInfo = pageElements.text();
                        // System.out.println(pageInfo);

                        String regex = "/[0-9]{0,}";
                        Pattern pattern = Pattern.compile(regex);
                        Matcher matcher = pattern.matcher(pageInfo);
                        String all = "";
                        if(matcher.find()) {
                            all = matcher.group();
                            all = all.replaceAll("/","");
                            total_page =  Integer.valueOf(all);
                            System.out.println("总页数"+all);
                        }*/

                        flag = false;

                    } catch (Exception e) {
                        logger.error(e.getMessage(),e);
                    }
                    //}

                    //ArrayList<ArrayList<Integer>> splitPages = CommonUtils.splitPages(2, total_page, 50);
                   /*if(total_page>=2) {
                        for (int k =2;k<=total_page;k++) {
                            String new_url = module_url  + "&page=" + k;
                            long future1 = System.currentTimeMillis() + Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                            boolean flag1 = true;
                            long remianing1 = Long.valueOf(PropertiesUtil.getVal("KaiDi.GetForumUrlTime"));
                            Elements threadList1 = null;
                            Document document1 = null;
                            //while (flag1 && remianing1 >= 0) {
                            try {
                                remianing1 = future1 - System.currentTimeMillis();
                                Page page = null;
                                page = getHtml(new_url,  ProxyPool.proxy());
                                if (page != null) {
                                    if (page.getStatusCode() != 200 || page.getRawText() == null) {
                                        System.out.println(page.getStatusCode());
                                        continue;
                                    } else {
                                        document1 = page.getHtml().getDocument();
                                    }
                                } else {
                                    continue;
                                }
                                threadList1 = document1.select(".wrapper").select(".list-table").select("tr[name^='showreply_']");
                                for (int i = 0; i < threadList1.size(); i++) {
                                    String forumUrl = threadList1.get(i).select(".subject").select(".f14px").select("a").get(0).attr("href");
                                    KaiDiTask task = new KaiDiTask(URLDecoder.decode(forumUrl, "UTF-8"));
                                    CommonTaskManager.addTask("KaiDiForum", task,1000);
                                    System.out.println(forumUrl);
                                }
                                flag1 = false;
                            } catch (Exception e) {
                                logger.error("解析模块中其他页内容错误", e);
                                continue;
                            }
                            // }
                        }
                    }*/


                    /*ArrayList<ArrayList<Integer>> taskSplit = CommonUtils.splitPages(1, elementsList.size(), 5);
                    for(int j = 0;j<taskSplit.size();j++){
                        KaiDiDownLoader.ModuleCrawler crawler = new KaiDiDownLoader.ModuleCrawler(elementsList, taskSplit.get(j));
                        pool.execute(crawler);//提交任务
                    }*/
                }
            }catch (Exception e){
                logger.error(e.getMessage(),e);
                continue;
            }
        }
    }

}
